package com.ciwong.epaper.modules.reciteWords.ui;

import android.app.Activity;
import android.content.Context;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.modules.evaluate.bean.WordDetail;
import com.ciwong.epaper.modules.reciteWords.util.WordJumpManager;
import com.ciwong.libs.audio.play.AudioPlayer;

import java.util.ArrayList;

import static com.ciwong.epaper.modules.reciteWords.ui.ReciteWordDetailActivity.INTENT_FROM_WORD_STUDY_ACTIVITY;

public class EnhlishToChinese extends StudyWordMoudleView implements View.OnClickListener {

    @Override
    public void updataui() {
        isLock=true;
        item_1_result.setVisibility(GONE);
        item_2_result.setVisibility(GONE);
        item_3_result.setVisibility(GONE);
        item_4_result.setVisibility(GONE);

        item_1.setBackground(getResources().getDrawable(R.drawable.item_default_shape));
        item_2.setBackground(getResources().getDrawable(R.drawable.item_default_shape));
        item_3.setBackground(getResources().getDrawable(R.drawable.item_default_shape));
        item_4.setBackground(getResources().getDrawable(R.drawable.item_default_shape));
    }

    private Context mContex;
    private WordDetail currentWord;
    private ArrayList<WordDetail> wordDetails=new ArrayList<WordDetail>();

    private ImageView item_1_result,item_2_result,item_3_result,item_4_result;
    private TextView item_1,item_2,item_3,item_4,word_name,word_yibiao;
    private NextStepLinstener nextStepLinstener;
    private boolean isLock=true;

    public EnhlishToChinese(Context context) {
        super(context);
        mContex=context;

        LayoutInflater.from(this.getContext()).inflate(R.layout.english_to_chinese, this);
        initView();
    }

    public EnhlishToChinese(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContex=context;

        LayoutInflater.from(this.getContext()).inflate(R.layout.english_to_chinese, this);
        initView();
    }

    public EnhlishToChinese(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContex=context;

        LayoutInflater.from(this.getContext()).inflate(R.layout.english_to_chinese, this);
        initView();
    }


    private void initView(){
        setOrientation(VERTICAL);
        item_1=findViewById(R.id.item_1);
        item_2=findViewById(R.id.item_2);
        item_3=findViewById(R.id.item_3);
        item_4=findViewById(R.id.item_4);
        item_1_result=findViewById(R.id.item_1_result);
        item_2_result=findViewById(R.id.item_2_result);
        item_3_result=findViewById(R.id.item_3_result);
        item_4_result=findViewById(R.id.item_4_result);
        word_name=findViewById(R.id.word_name);
        word_yibiao=findViewById(R.id.word_yibiao);

        item_1.setOnClickListener(this);
        item_2.setOnClickListener(this);
        item_3.setOnClickListener(this);
        item_4.setOnClickListener(this);


    }


    public void SteDataAndUpdataUi(WordDetail currentWord,ArrayList<WordDetail> wordDetails,  NextStepLinstener nextStepLinstener){

        try{
            if(currentWord==null||wordDetails.size()<4){
                return;
            }
            isLock=true;
            this.nextStepLinstener=nextStepLinstener;
            this.currentWord=currentWord;
            this.wordDetails=wordDetails;
            word_name.setText(currentWord.getWords());
            word_yibiao.setText("["+currentWord.getSymbol()+"]");
            item_1.setText(wordDetails.get(0).getPretations().replace('\n',' ').replace('\r',' ').replace('\t',' '));
            item_2.setText(wordDetails.get(1).getPretations().replace('\n',' ').replace('\r',' ').replace('\t',' '));
            item_3.setText(wordDetails.get(2).getPretations().replace('\n',' ').replace('\r',' ').replace('\t',' '));
            item_4.setText(wordDetails.get(3).getPretations().replace('\n',' ').replace('\r',' ').replace('\t',' '));

            item_1_result.setVisibility(GONE);
            item_2_result.setVisibility(GONE);
            item_3_result.setVisibility(GONE);
            item_4_result.setVisibility(GONE);

            item_1.setBackground(getResources().getDrawable(R.drawable.item_default_shape));
            item_2.setBackground(getResources().getDrawable(R.drawable.item_default_shape));
            item_3.setBackground(getResources().getDrawable(R.drawable.item_default_shape));
            item_4.setBackground(getResources().getDrawable(R.drawable.item_default_shape));

        }catch (Exception e){
            e.getStackTrace();
            nextStepLinstener.nextStep("ENGLISH_TO_CHINESE");
        }

    }
    private Handler mHandler = new Handler();

    @Override
    public void onClick(View view) {
        try{
            if(view.getId()==R.id.item_1){
                if(isLock){
                    if(currentWord.getdId().equals(wordDetails.get(0).getdId())){
                        isLock=false;
                        item_1_result.setVisibility(VISIBLE);
                        item_1_result.setImageResource(R.mipmap.right_img2);
                        item_1.setBackground(getResources().getDrawable(R.drawable.item_right_shape));
                        //跳转下一题
                        AudioPlayer.getInstance().play(WordStudyActivity.SOUND_RIGHT, mContex, null);
                        mHandler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                nextStepLinstener.nextStep("ENGLISH_TO_CHINESE");

                            }
                        }, DELAY_TIME);


                    }else {
                        isLock=false;
                        item_1_result.setVisibility(VISIBLE);
                        item_1_result.setImageResource(R.mipmap.error_img);
                        item_1.setBackground(getResources().getDrawable(R.drawable.item_error_shape));
                        //跳转详情页
                        AudioPlayer.getInstance().play(WordStudyActivity.SOUND_ERROR, mContex, null);
                        //跳转详情
                        mHandler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                WordJumpManager.jumpToReciteWordDetailActivity(R.string.go_back,(Activity) mContex,INTENT_FROM_WORD_STUDY_ACTIVITY,null,currentWord.getdId());

                            }
                        }, DELAY_TIME);
                    }
                }

            }else if(view.getId()==R.id.item_2){
                if(isLock){
                    if(currentWord.getdId().equals(wordDetails.get(1).getdId())){
                        isLock=false;
                        item_2_result.setVisibility(VISIBLE);
                        item_2_result.setImageResource(R.mipmap.right_img2);
                        item_2.setBackground(getResources().getDrawable(R.drawable.item_right_shape));
                        //跳转下一题
                        AudioPlayer.getInstance().play(WordStudyActivity.SOUND_RIGHT, mContex, null);
                        mHandler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                nextStepLinstener.nextStep("ENGLISH_TO_CHINESE");

                            }
                        }, DELAY_TIME);
                    }else {
                        isLock=false;
                        item_2_result.setVisibility(VISIBLE);
                        item_2_result.setImageResource(R.mipmap.error_img);
                        item_2.setBackground(getResources().getDrawable(R.drawable.item_error_shape));
                        //跳转详情页
                        AudioPlayer.getInstance().play(WordStudyActivity.SOUND_ERROR, mContex, null);
                        //跳转详情
                        mHandler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                WordJumpManager.jumpToReciteWordDetailActivity(R.string.go_back,(Activity) mContex,INTENT_FROM_WORD_STUDY_ACTIVITY,null,currentWord.getdId());

                            }
                        }, DELAY_TIME);
                    }
                }


            }else if(view.getId()==R.id.item_3){
                if(isLock){
                    if(currentWord.getdId().equals(wordDetails.get(2).getdId())){
                        isLock=false;
                        item_3_result.setVisibility(VISIBLE);
                        item_3_result.setImageResource(R.mipmap.right_img2);
                        item_3.setBackground(getResources().getDrawable(R.drawable.item_right_shape));
                        //跳转下一题
                        AudioPlayer.getInstance().play(WordStudyActivity.SOUND_RIGHT, mContex, null);
                        mHandler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                nextStepLinstener.nextStep("ENGLISH_TO_CHINESE");

                            }
                        }, DELAY_TIME);
                    }else {
                        isLock=false;
                        item_3_result.setVisibility(VISIBLE);
                        item_3_result.setImageResource(R.mipmap.error_img);
                        item_3.setBackground(getResources().getDrawable(R.drawable.item_error_shape));
                        //跳转详情页
                        AudioPlayer.getInstance().play(WordStudyActivity.SOUND_ERROR, mContex, null);
                        //跳转详情
                        mHandler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                WordJumpManager.jumpToReciteWordDetailActivity(R.string.go_back,(Activity) mContex,INTENT_FROM_WORD_STUDY_ACTIVITY,null,currentWord.getdId());

                            }
                        }, DELAY_TIME);
                    }
                }


            }else if(view.getId()==R.id.item_4){
                if(isLock){
                    if(currentWord.getdId().equals(wordDetails.get(3).getdId())){
                        isLock=false;
                        item_4_result.setVisibility(VISIBLE);
                        item_4_result.setImageResource(R.mipmap.right_img2);
                        item_4.setBackground(getResources().getDrawable(R.drawable.item_right_shape));
                        //跳转下一题
                        AudioPlayer.getInstance().play(WordStudyActivity.SOUND_RIGHT, mContex, null);
                        mHandler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                nextStepLinstener.nextStep("ENGLISH_TO_CHINESE");

                            }
                        }, DELAY_TIME);
                    }else {
                        isLock=false;
                        item_4_result.setVisibility(VISIBLE);
                        item_4_result.setImageResource(R.mipmap.error_img);
                        item_4.setBackground(getResources().getDrawable(R.drawable.item_error_shape));
                        //跳转详情页
                        AudioPlayer.getInstance().play(WordStudyActivity.SOUND_ERROR, mContex, null);
                        //跳转详情
                        mHandler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                WordJumpManager.jumpToReciteWordDetailActivity(R.string.go_back,(Activity) mContex,INTENT_FROM_WORD_STUDY_ACTIVITY,null,currentWord.getdId());

                            }
                        }, DELAY_TIME);
                    }

                }

            }

        }catch (Exception e){
            e.getStackTrace();
            nextStepLinstener.nextStep("ENGLISH_TO_CHINESE");
        }



    }
}
